<p>When working with collections that are known to be non-empty, using <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.first">First</a> or <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single">Single</a> is generally preferred over <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault">FirstOrDefault</a> or <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.singleordefault">SingleOrDefault</a>.</p>
<h2>Why is this an issue?</h2>
<p>Using <code>FirstOrDefault</code> or <code>SingleOrDefault</code> on collections that are known to be non-empty is an issue due to:</p>
<ul>
  <li> Code Clarity and intent: When you use <code>FirstOrDefault</code> or <code>SingleOrDefault</code>, it implies that the collection might be
  empty, which can be misleading if you know it is not. It can be confusing for other developers who read your code, making it harder for them to
  understand the actual constraints and behavior of the collection. This leads to confusion and harder-to-maintain code. </li>
  <li> Error handling: If the developer’s intend is for the collection not to be empty, using <code>FirstOrDefault</code> and
  <code>SingleOrDefault</code> can lead to subtle bugs. These methods return a default value (<code>null</code> for reference types and
  <code>default</code> for value types) when the collection is empty, potentially causing issues like <code>NullReferenceException</code> later in the
  code. In contrast, <code>First</code> or <code>Single</code> will throw an <code>InvalidOperationException</code> immediately if the collection is
  empty, making it easier to detect and address issues early in the development process. </li>
  <li> Code coverage: Potentially, having to check if the result is <code>null</code>, you introduces a condition that cannot be fully tested,
  impacting the code coverage. </li>
</ul>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
var items = new List&lt;int&gt; { 1, 2, 3 };

int firstItem = items.FirstOrDefault(); // Noncompliant, this implies the collection might be empty, when we know it is not
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
var items = new List&lt;int&gt; { 1, 2, 3 };

int firstItem = items.First(); // Compliant
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single"><code>Single</code></a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.first"><code>First</code></a> </li>
  <li> Microsoft Learn - <a
  href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.singleordefault"><code>SingleOrDefault</code></a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault"><code>FirstOrDefault</code></a>
  </li>
</ul>
<h3>Articles &amp; blog posts</h3>
<ul>
  <li> <a href="https://medium.com/@anyanwuraphaelc/first-vs-firstordefault-single-vs-singleordefault-a-high-level-look-d24db17a2bc3">First vs
  FirstOrDefault, Single vs SingleOrDefault: A High-level Look</a> </li>
</ul>

